window.onload=function(){
    let date = new Date();
    let year=date.getFullYear();
    let month=date.getMonth()+1;
    date = new Date(date.getFullYear()+'/'+(date.getMonth()+1)+'/'+1);
    // date = new Date('2022/2/1');
    init(date)
    $('.month').click(function(){
        init(date)
        $('.down').removeAttr('disabled');
        $('.on').removeAttr('disabled');
        $('.date_mot').css('display','table')
        $('.data_week').css('display','none')
        $('.date_day').css('display','none')
    })
    $('.week').click(function(){
        initw(date)
        $('.on').attr('disabled','disabled')
        $('.down').attr('disabled','disabled')
        $('.date_mot').css('display','none')
        $('.data_week').css('display','table')
        $('.date_day').css('display','none')
    })
    $('.day').click(function(){
        initd(date)
        $('.down').attr('disabled','disabled')
        $('.on').attr('disabled','disabled')
        $('.date_mot').css('display','none')
        $('.data_week').css('display','none')
        $('.date_day').css('display','table')
    })
    $('.on').click(function(){
        --month;
        let dates;
        if(month<=0){
            month=12;
            year--;
            dates = new Date(year+'/'+month+'/'+1);
        }else{
            dates = new Date(year+'/'+month+'/'+1);
        }
        init(dates)
    })
    $('.down').click(function(){
        ++month;
        let dates;
        if(month>12){
            month=1;
            year++;
            dates = new Date(year+'/'+month+'/'+1);
        }else{
            dates = new Date(year+'/'+month+'/'+1);
        }
        init(dates)
    })
}
function init(date){
    $('.date').text(date.getFullYear()+'年'+(date.getMonth()+1)+'月')
    let day = days(date.getFullYear(),date.getMonth()+1);
    riliy(date.getFullYear(),date.getMonth()+1,day,date.getDay())
    week(date.getFullYear(),date.getMonth()+1,20)
}

function initw(date){
    w=week(date.getFullYear(),date.getMonth()+1,20)
    riliw()
    $('.date').text(date.getFullYear()+'年第'+w+'周')
}
function initd(date){
    rilid()
    $('.date').text(date.getFullYear()+'年'+(date.getMonth()+1)+'月'+new Date().getDate()+'日')
}
function riliy(year,month,day,week){
    let date = new Date();
    let mon=new Date().getMonth()+1==month&&year==new Date().getFullYear();
    $('.date_mot>tbody').text('')
    date = date.getDate()
    let nowday=1;
    let space=0;
    for(let i=0;i<Math.ceil((day+week)/7);i++){
        let tr = document.createElement('tr')
        for(let j=0;j<7;j++){
            
            if(space>=week-1){
                if(nowday<=day){
                    $(tr).append(`
                        <td><span>${nowday<10?'0'+nowday:nowday}</span><span></span><div class=${nowday==date?(mon?'today':''):''}>${nowday==date?(mon?'今天':'正常'):'正常'}</div></td>
                    `)
                    nowday++
                }else{
                    $(tr).append(`
                        <td><span></span><span></span><div class='null'>正常</div></td>
                    `)
                    space--
                }
            }else{
                $(tr).append(`
                    <td><span></span><span></span><div class='null'>正常</div></td>
                `)
                space++
            }
        }
        $('.date_mot>tbody').append(tr)
    }
}
function riliw(){
    let week=[]
    $('.data_week>thead').text('')
    $('.data_week>tbody').text('')
    let date = new Date();
    let c=date.getDay()-1;
    c=date.getDate()-c;
    for(let i=0;i<7;i++){
        week.push(c+i)
    }
    $('.data_week>thead').append(`
        <tr>
            <td>时间</td>
            <td>周一<span>${date.getMonth()+1}/${week[0]}</span></td>
            <td>周二<span>${date.getMonth()+1}/${week[1]}</span></td>
            <td>周三<span>${date.getMonth()+1}/${week[2]}</span></td>
            <td>周四<span>${date.getMonth()+1}/${week[3]}</span></td>
            <td>周五<span>${date.getMonth()+1}/${week[4]}</span></td>
            <td>周六<span>${date.getMonth()+1}/${week[5]}</span></td>
            <td>周日<span>${date.getMonth()+1}/${week[6]}</span></td>
        </tr>
    `)
    for(let i=0;i<24;i++){
        $('.data_week>tbody').append(`
            <tr>
                <td>${i==0?'全天':(i<10?'0'+i:i)}</td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
            </tr>
        `)
    }
}
function rilid(){
    let date = new Date();
    let day =date.getDay();
    $('.date_day>thead').text('')
    $('.date_day>tbody').text('')
    switch(day){
        case 1:day='一';break;
        case 2:day='二';break;
        case 3:day='三';break;
        case 4:day='四';break;
        case 5:day='五';break;
        case 6:day='六';break;
        case 7:day='天';break;
    }
    $('.date_day>thead').append(`
        <tr>
            <td>时间</td>
            <td>周${day}<span>${date.getMonth()+1}/${date.getDate()}</span></td>
        </tr>
    `)
    for(let i=0;i<24;i++){
        $('.date_day>tbody').append(`
            <tr>
                <td>${i==0?'全天':(i<10?'0'+i:i)}</td>
                <td></td>
            </tr>
        `)
    }
}
function days(year,month){
    let days=0;
    switch(month){
        case 1:;
        case 3:;
        case 5:;
        case 7:;
        case 8:;
        case 10:;
        case 12: days=31;break;
        case 2:;
        case 4:;
        case 6:;
        case 9:;
        case 11: days=30;break;
    }
    if(month==2){
        if((year%4==0&&year%100!=0)||year%400==0){
            days=29
        }else{
            days=28
        }
    }
    return days;
}
function week(year,month,day){
    console.log(year,month,day);
    let date = new Date(year+'/'+month+'/'+1);
    date = date.getDay();
    let j=1
    for(;date<=7;j++){
        date++
    }
        let rn = 0;
        let all = 0;
        if((year%4==0&&year%100!=0)||year%400==0){
                rn=1;
        }
        for(let i=1;i<month;i++){
            switch(i){
            case 1:;
            case 3:;
            case 5:;
            case 7:;
            case 8:;
            case 10:;
            case 12: all+=31;break;
            case 2:;
            case 4:;
            case 6:;
            case 9:;
            case 11: all+=30;break;
        }
        }
        if(month>2&&rn==1){
            all-=1
        }else if(month>2){
            all-=2
        }
        all+=day-j;
        let i=1
        for(;all>=0;i++){
            all-=7;
        }
        return i;
}